<template>
  <div style="height: 100%; min-height: 100vh">
    <div class="cus_search">
      <div class="cus_search_left" style="width: calc(100% - 42px)">
        <img src="@/assets/img/search for@2x.png" alt=""/>
        <input type="text" v-model="value" @focus="goSearch"/>
        <img src="@/assets/img/delete@2x.png" alt="" v-if="value.length > 0" @click="emptyValue"/>
      </div>
      <div class="cus_search_right" @click="sizerFun">
        <img src="@/assets/img/shaixuan@2x.png" alt=""/>
      </div>
    </div>
    <div class="appo_doctor">
      <div class="appo_doctor_list" id="wrapper" @scroll="scrollGet($event)">
        <div v-for="(item, index) in doctorList" :id="item.pinyinCode">
          <div class="pinyinBox">
            {{ item.pinyinCode }}
          </div>
          <!-- <div v-for="(even, i) in item.children" class="doctor_list" @click="goDoctorIntroduction(even)">

            <img src="@/assets/img/img_male.png" alt="" v-if="even.sex == '1'" />
            <img src="@/assets/img/icon_female.png" alt="" v-else />
            <div>
              <div>{{ even.name }}</div>
              <div>{{ even.deptName }} | {{ even.jobTitle }}</div>
              <div style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              ">
                {{ even.desc }}
              </div>
            </div>
          </div> -->
          <div class="doctor_item" v-for="(even, i) in item.children" @click="goDoctorIntroduction(even)">
            <div class="doctor_item_left">
              <img :src="even.avatarUrl?even.avatarUrl:require('@/assets/img/img_male.png')" :onerror="errorImg01"
                   alt="" v-if="even.sex == '1'"/>
              <img :src="even.avatarUrl?even.avatarUrl:require('@/assets/img/icon_female.png')" :onerror="errorImg02"
                   alt="" v-else/>
            </div>
            <div class="doctor_item_right">
              <div class="doctor_item_right_1">
                {{ even.name }} <span v-if="even.team">{{ even.team }}</span>
              </div>
              <div class="doctor_item_right_2">
                {{ even.jobTitle }}
              </div>
              <div class="doctor_item_right_3" v-if="even.memo.length>0">
               <span v-for="i in even.memo">
                  {{ i }}
                </span>

              </div>
              <div class="doctor_item_right_5" v-if="even.desc">

                {{ even.desc }}
              </div>

            </div>
          </div>
        </div>

      </div>
      <div class="appo_doctor_model" v-if="sizerShow">
        <div class="appo_box">
          <div class="appo_box_dep">
            <div class="dep" v-for="(value, index) in departmentList" @click="screenClick" :data-id="value.deptId"
                 :style="{
                'background-color': idx == value.deptId ? '#F1F5F9' : '#fff',
              }">
              {{ value.deptName }}
            </div>
          </div>
          <!-- <div class="appo_box_room">
            <div
              class="room"
              v-for="(value, index) in room"
              @click="screenClick2(value.deptId)"
            >
              {{ value.deptName }}
            </div>
          </div> -->
        </div>
      </div>
      <div class="indexes">
        <div v-for="item in FirstPin" @click="moveFun(item)" :class="{ divActive: item == pyCode }">
          {{ item }}
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {getDoctorInfo, getDeptInfo} from "@/api/api";
import {getToken, removeToken, removeWxUserinfoCookies} from "@/api/auth";

export default {
  components: {},
  data() {
    return {
      value: "",
      idx: null,
      pyCode: "",
      departmentList: [
        // { name: "内科" },
        // { name: "外科" },
        // { name: "儿科" },
        // { name: "妇产科" },
        // { name: "耳鼻科/耳鼻科耳鼻科" }
      ],
      FirstPin: [
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "J",
        "K",
        "L",
        "M",
        "N",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "W",
        "X",
        "Y",
        "Z"
      ],

      room: [],
      sizerShow: false,
      doctorList: [],
      errorImg01: 'this.src="' + require("@/assets/img/img_male.png") + '"',
      errorImg02: 'this.src="' + require("@/assets/img/icon_female.png") + '"',
    };
  },
  created() {
    this.GetDeptInfo();
    // setTimeout(()=>{
    //     this.GetDeptInfoNext(this.departmentList[0].deptId)
    // },500)
    this.GetHosDoctor();


  },
  methods: {
    // 医生列表
    GetHosDoctor(deptId) {
      var data = {
        deptId: deptId || "",
        doctorId: "",
      };
      getDoctorInfo(data).then((res) => {
        if (res.code == 200) {
          var resData = res.data, list = []
          resData.forEach(element => {
            if (element.memo) {
              element.memo = element.memo.split("|")
            } else {
              element.memo = []
            }
          });
          resData.forEach((element, index, arr) => {
            if (index == 0) {
              list.push({
                pinyin: 'sort',

                firstPinyin: arr[index].firstPinyin,
                pinyinCode: arr[index].firstPinyin,
                children: []
              })

            } else if (arr[index - 1].firstPinyin != arr[index].firstPinyin) {
              list.push({
                id: 'sort',
                pinyinCode: arr[index].firstPinyin,
                firstPinyin: arr[index].firstPinyin,
                children: []
              })
              // list[index].children.push(element)
            }
          });
          list.forEach((element1, index1) => {
            res.data.forEach((element2, index2) => {
              if (element1.firstPinyin == element2.firstPinyin) {
                list[index1].children.push(element2)
              }
            });
          });
          console.log(list)

          this.doctorList = list;
        }
      });
    },
    moveFun(item) {
      this.pyCode = item
      let div = document.getElementById(item); // 指定的元素
      let wrapper = document.getElementById("wrapper");
      console.log(div.offsetTop)
      wrapper.scrollTo(0, div.offsetTop);
    },
    scrollGet(e) {
      // console.log(e.target.scrollTop)
      let scrollTop = e.target.scrollTop
      this.doctorList.forEach((element, index, arr) => {

        let offsetTop = document.getElementById(element.pinyinCode).offsetTop;
        // 获取监听元素本身高度
        let scrollHeight = document.getElementById(element.pinyinCode).scrollHeight;
        if (scrollTop > offsetTop && scrollTop < (offsetTop + scrollHeight)) {
          this.pyCode = element.pinyinCode
        }


      });
    },
    // 一级科室
    GetDeptInfo() {
      const data = {
        deptId: "",
        level: "1",
      };
      getDeptInfo(data).then((res) => {
        if (res.code == 200) {
          this.departmentList = res.data;
        }
      });
    },
    // 二级科室
    GetDeptInfoNext(PDeptId) {
      const data = {
        PDeptId,
      };
      this.$api.regController.GetDeptInfo(data).then((res) => {
        if (res.code == 200) {
          this.room = res.data;
        }
      });
    },
    clearFun() {
      try {
        console.log("清理localStorage");
        localStorage.clear();
      } catch (err) {
        console.log("清理localStorage失败:" + err);
      }
      try {
        console.log("清理token");
        removeToken();
        // removeWxUserinfoCookies();
      } catch (err) {
        alert(err)
        console.log(err);
      }
    },
    isLoginAndIsband() {
      const that = this;
      if (!getToken()) {
        that.$vux.confirm.show({
          // 组件除show外的属性
          content: "未登录,请先登录账号",
          onCancel() {
          },
          onConfirm() {
            that.clearFun()
            that.$router.push({path: "/"});
          },
        });
        return false;
      } else {
        if (window.localStorage.getItem("isband") == "false") {
          this.$vux.confirm.show({
            // 组件除show外的属性
            content: "请添加就诊人",
            onCancel() {
            },
            onConfirm() {
              that.$router.push({path: "/my_patient"});
            },
          });
          return false;
        } else {
          return true;
        }
      }
    },
    // 跳转医生详情页
    async goDoctorIntroduction(item) {
      const that = this;
      var or = await that.isLoginAndIsband();
      if (or) {
        window.localStorage.removeItem("appoIdx")
        that.$router.push({path: "/doctor_details", query: item});
      }
    },
    // 跳转医生搜索页
    goSearch() {
      this.$router.push({path: "/doctor_search"});
    },

    emptyValue() {
      this.value = "";
    },
    screenClick(e) {
      console.log(e.target.dataset.id);
      this.idx = e.target.dataset.id;
      this.GetHosDoctor(e.target.dataset.id);
      this.sizerFun();
      //   this.GetDeptInfoNext(e.target.dataset.id);
    },
    screenClick2(deptId) {
      this.sizerFun();
    },
    sizerFun() {
      this.sizerShow = !this.sizerShow;
    },
  },
  filters: {
    memoTo(value) {
      var str = value.replaceAll("|", " | ")
      return str
    },
  }
};
</script>
<style scoped>
.cus_search {
  width: calc(100% - 2rem);
  height: 2.125rem;
  padding: 0.3125rem 1rem;
  display: flex;
  align-items: center;
  font-size: 0.9375rem;
  color: #131a2a;
}

.cus_search_left {
  width: calc(100% - 1.125rem);
  height: 2.125rem;
  padding: 0 0.5625rem;
  display: flex;
  align-items: center;
  background: rgba(247, 250, 253, 1);
  border-radius: 1.125rem;
}

.cus_search_left > img {
  width: 1.375rem;
  height: 1.375rem;
}

.cus_search_left > img:nth-of-type(1) {
  margin-right: 0.375rem;
}

.cus_search_left > input {
  width: calc(100% - 3.125rem);
  padding: 0px 0.1875rem;
  height: 100%;
  border: none;
  background: rgba(247, 250, 253, 1);
  outline: none;
}

.cus_search_right {
  width: 2.625rem;
  text-align: right;
}

.cus_search_right img {
  width: 1.375rem;
  height: 1.375rem;
}

.pinyinBox {
  line-height: 2.25rem;
  font-size: 1rem;
  font-weight: bold;
  color: rgb(111, 111, 111);
}

.indexes {
  width: 1.5625rem;
  text-align: center;
  height: 80vh;
  position: fixed;
  right: 1rem;
  top: 10vh;
  font-size: 0.875rem;
  font-weight: bold;
  z-index: 100000;

}

.indexes div {

  line-height: 1.5625rem;

}

.indexes .divActive {
  background-color: rgb(47, 255, 75);
  border-radius: 50%;
  color: #fff;
}


.indexes div a:hover {
  color: rgb(47, 255, 75);
}

.appo_doctor {
  height: calc(100vh - 44px);
  position: relative;
}

.appo_doctor_list {
  width: calc(100% - 2rem);
  padding: 1rem;
  height: calc(100% - 2rem);
  overflow-y: scroll;
  background-color: #f1f5f9;
}

.doctor_item {
  width: calc(100% - 1.625rem);
  margin-bottom: 0.75rem;
  padding: 0.75rem 0.8125rem;
  font-size: 0.875rem;
  box-shadow: 0.00rem 0.00rem 0.9375rem 0.00rem rgba(174, 206, 254, 0.3);
  border-radius: 0.375rem;
  background-color: #fff;
  position: relative;
  display: flex;

}

.doctor_item .doctor_item_left {
  width: 4.25rem;
  margin-right: 0.625rem;


}

.doctor_item .doctor_item_left > img {
  width: 4.25rem;
  height: 4.25rem;
  margin-right: 0.625rem;
}


.doctor_item .doctor_item_right {
  width: calc(100% - 5rem);
  font-size: 0.8125rem;
}

.doctor_item .doctor_item_right .doctor_item_right_1 {
  line-height: 1.625rem;
  height: 1.625rem;
  font-size: 1.00rem;
  font-family: PingFang SC;
  font-weight: 500;
  color: #131A2A;
  display: flex;
  align-items: center;

}

.doctor_item .doctor_item_right .doctor_item_right_1 span {
  display: inline-block;
  padding: 0 0.44rem;
  height: 1.1875rem;
  line-height: 1.1875rem;
  background: rgba(255, 202, 127, 0.25);
  border-radius: 0.19rem;
  font-size: 0.75rem;
  font-family: PingFang SC;
  font-weight: 500;
  color: #FF9C32;
  margin-left: 0.625rem;
}

.doctor_item .doctor_item_right .doctor_item_right_2 {
  line-height: 1.5rem;
  height: 1.5rem;

}

.doctor_item .doctor_item_right .doctor_item_right_3 {
  line-height: 1.5rem;
  height: 1.5rem;


}

.doctor_item .doctor_item_right .doctor_item_right_3 span {
  display: inline-block;
  padding: 0.00rem 0.375rem;
  height: 1.19rem;
  line-height: 1.1875rem;
  background: rgba(68, 142, 255, .1);
  border-radius: 0.19rem;
  font-size: 0.75rem;
  color: #448EFF;
  margin-right: 0.75rem;
}

.doctor_item .doctor_item_right .doctor_item_right_5 {
  line-height: 1.5rem;
  height: 1.5rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #a4adbe;
}

.appo_doctor_model {
  width: 100%;
  position: absolute;
  top: 0px;
  left: 0;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6);
}

.appo_box {
  height: calc(100vh - 25rem);
  background-color: #f1f5f9;
  display: flex;
  font-size: 0.9375rem;
}

.appo_box_dep {
  width: 8.4375rem;
  height: 100%;
  overflow-y: scroll;
}

.appo_box_dep .dep {
  width: 6.3125rem;
  padding: 0.9375rem 1.0625rem;
  text-align: center;
  font-weight: bold;
  background: rgba(255, 255, 255, 1);
  border-radius: 0px 0.3125rem 0.3125rem 0px;
  margin-bottom: 0.0625rem;
}

.appo_box_room {
  width: calc(100% - 8.4375rem);
  height: 100%;
  overflow-y: scroll;
}

.appo_box_room .room {
  width: calc(100% - 2.125rem);
  padding: 0.9375rem 1.0625rem;
  text-align: center;
  margin-bottom: 0.0625rem;
}
</style>
